package cn.com.websocket.util;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtil{

    public static final String DATEFORMAT_STR_033 = "yyyy/MM/dd HH:mm:ss";

    public static final String YYYY_MM_DD = "yyyy-MM-dd";

    public static final String YYYYMMDD = "yyyyMMdd";

    public static final String YYYY_MM_DD_HH_MM_SS="yyyy-MM-dd HH:mm:ss";

    public static final String yyyyMMddHHmmss="yyyyMMddHHmmss";

    public static final String yyyyMMddhhmmssSSS="yyyyMMddhhmmssSSS";

    private final static int[] dayArr = new int[] { 20, 19, 21, 20, 21, 22, 23, 23, 23, 24, 23, 22 };

    @SuppressWarnings("deprecation")
    public static Date dateAddMins(Date date, int minCnt) {
        Date d = new Date(date.getTime());
        d.setMinutes(d.getMinutes() + minCnt);
        return d;
    }
    /**
     * String转化Date格式
     * @param date
     * @param type
     * @return
     */
    public static Date parse(String date,String type){
        SimpleDateFormat formatter = new SimpleDateFormat(type);
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(date, pos);
        return strtodate;

    }

    /**
     * <p>根据给条件、格式化时间</p>
     * @param dateDate
     * @param formatString
     * @return
     * */
    public static String dateToString(Date dateDate, String formatString) {
        String dateString = "";
        if (dateDate != null && formatString != "" && formatString != null) {
            SimpleDateFormat formatter = new SimpleDateFormat(formatString);
            dateString = formatter.format(dateDate);
        }
        return dateString;
    }

    public static String getNowDate(String formatString){
        return dateToString(new Date(),formatString);
    }

    /**
     * 获取当前时间前后beforeOrAfterDay天时间
     * @param beforeOrAfterDay
     * @return Date
     */
    public static Date getDate(int beforeOrAfterDay) {
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.add(GregorianCalendar.DATE, beforeOrAfterDay);
        return calendar.getTime();
    }

    /**
     * 获取Date前后afterOrAgo天时间
     * @param date
     * @param beforeOrAfterDay
     * @return Date
     */
    public static Date getDate(Date date, int beforeOrAfterDay) {
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(GregorianCalendar.DATE, beforeOrAfterDay);
        return calendar.getTime();
    }


    /**
     * 增加日期天数
     * 2017-05-06 to 2017-05-07
     * @param dateString 日期字符串
     * 					例： '2017-05-06'
     * @param day 天数
     * @return 增加后的日期  '2017-05-07'
     */
    public static String addDay(String dateString, int day) {
        Date date = parse(dateString, YYYY_MM_DD);
        date = getDate(date, day);
        return dateToString(date, YYYY_MM_DD);
    }

    /**
     * <p>根据给定时间获取当前年</p>
     * @param date
     * @return int
     * */
    public static int getYear(Date date){
        return getCalendarFieldValue(date, Calendar.YEAR);
    }

    /**
     * <p>根据给定时间获取当前月份</p>
     * @param date
     * @return int
     * */
    public static int getMonth(Date date){
        return getCalendarFieldValue(date, Calendar.MONTH)+1;
    }

    /**
     * <p>根据给定时间获取当前第几天</p>
     * @param date
     * @return int
     * */
    public static int getDayOfMohth(Date date){
        return getCalendarFieldValue(date, Calendar.DAY_OF_MONTH);
    }

    /**
     * 根据给定条件获取日历字段的值
     * @param date 待处理的时间
     * @param field (Calendar.YEAR,Calendar.MONTH....)
     * @return
     * */
    private static int getCalendarFieldValue(Date date,int field){
        try {
            GregorianCalendar calendar = new GregorianCalendar();
            calendar.setTime(date);
            return calendar.get(field);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

}